package com.example.lanouhn.bantangapp.activities.utils;



import com.example.lanouhn.bantangapp.activities.contants.Selected_home;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析Json数据
 */
public class JsonUtils {

    /**
     * @param json json字符串
     * @return 精选列表
     */
    public static List<Selected_home> getHomeList(String json) {
        List<Selected_home> list = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(json);

            JSONObject jsonObject1= jsonObject.getJSONObject("data");
            JSONArray jsonArray = jsonObject1.getJSONArray("topic");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject j = jsonArray.getJSONObject(i);

                Selected_home m = new Selected_home();
                m.setTitle(j.getString("title"));
                m.setPic(j.getString("pic"));
                m.setLikes(j.getString("likes"));
                m.setComments(j.getString("comments"));
                list.add(m);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
